package org.ricks.condition1;

import org.ricks.condition1.event.ProgressUpdatedEvent;
import org.ricks.ioc.event.EventBus;
import org.ricks.ioc.event.IEvent;

import java.util.function.Consumer;

//通过策略接口解耦事件处理逻辑，允许灵活定义进度更新方式。
public class GenericProgressCondition<E extends IEvent> extends ProgressCondition<E> {
    private final ProgressUpdateStrategy<E> updateStrategy;

    public GenericProgressCondition(int targetValue, Class<E> eventType,
                                    ProgressUpdateStrategy<E> updateStrategy,
                                    Consumer<ProgressCondition<E>> onDone) {
        super(targetValue, eventType, onDone);
        this.updateStrategy = updateStrategy;
    }

    @Override
    protected void handleEvent(E event) {
        int newValue = updateStrategy.update(currentValue, event);
        if (newValue != currentValue) {
            currentValue = newValue;
            // 触发进度更新事件
            EventBus.post(new ProgressUpdatedEvent<>(this, currentValue));
        }
        System.err.println("正在更新任务进度，curValue = " +  currentValue);
    }
}
